<?php

/**
 * Model CustomerUser
 *
 * @author nguyenmanhluu
 * @since Sep 19, 2013 - 12:59:02 AM
 */
class CustomerUser extends Users
{

    public function rules()
    {
        return array(
            array('email', 'required'),
            array('email', 'email'),
            array('email', 'validateMultiCustomer'),
            array('author_id', 'numerical'),
            array('password, new_pass, photo', 'length', 'min' => 6),
            array('firstname, lastname, password, repassword', 'length', 'max' => 32),
            array('status, postal_code, gender, business_id', 'length', 'max' => 20),
            array('type, address, business_name, business_type, website, contract_start, 
                contract_end, facebook, twitter, gplus, instagram',
                'length', 'max' => 255
            ),
            array('city, tax_id, fax', 'length', 'max' => 30),
            // Customer
            array(
                'email, new_pass, repassword, firstname, lastname',
                'required',
                'on' => 'create'
            ),
            array('repassword', 'compare', 'compareAttribute' => 'new_pass', 'on' => 'create'),
            array(
                'email, firstname, lastname',
                'required',
                'on' => 'edit'
            ),
            array(
                'email, new_pass, firstname, lastname',
                'unsafe',
                'on' => 'edit'
            ),
        );
    }

    public function validateMultiCustomer($attribute, $params)
    {
        $user         = self::model()->find("email=:e", array(":e" => $this->$attribute));
        $errorMessage = Yii::t('main', 'Email has already belongs to this Business!');

        if ($user != null)
        {
            if ($user->type != self::TYPE_CUSTOMER)
            {
                $this->addError($attribute, $errorMessage);
            }
            else
            {
                //kiem tra xem user co thuoc biz khong
                $check_cus_biz = Customer::checkCustomerBelongToBiz($user->id, $this->biz_id);

                if ($check_cus_biz)
                {
                    $this->addError($attribute, $errorMessage);
                }
            }
        }
    }

}

/** 
 * End file CustomerUser
 */
